package cpr;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import cpr.context.CommandContext;
import cpr.context.Context;

abstract public class DefaultCommandHandler implements CommandHandler {
	private CommandContext ctx;
	private boolean startInSeparateThread;
	private String commandName;
	private String[] commandNameParts;
	private Pattern spacePattern = Pattern.compile( "\\s" );
	private String quickInfoString,usageString, detailedHelp;
	private Map props;
	private Map externalProps;

	public DefaultCommandHandler(String name, String quickInfo) {
		this(name, quickInfo, null,null);
	}
	public DefaultCommandHandler(String name, String quickInfo, String usage, String detailedHelp) {
		commandName = name;
		commandNameParts = spacePattern.split(name);
		props = new HashMap();
		externalProps = new HashMap();
		setQuickInfo(quickInfo);
		startInSeparateThread = true;
	}
	public DefaultCommandHandler(String name) {
		this(name,null,null,null);
	}
	public String getQuickInfo() {
		
		return quickInfoString;
	}
	
	public void setQuickInfo(String quickInfo) {
		quickInfoString = quickInfo;
	}
	
	public void setUsage(String u) {
		usageString = u;
	}
	public String getUsage() {
		return usageString;
	}
	public String getDetailedHelp(){
		return detailedHelp;
	}
	
	public String getName() {
		
		return commandName;
	}
	public String getParameter(String[] params, int p, String defaultValue) {
		int pp = commandNameParts.length+p;
		if (params.length<pp) return defaultValue;
		return params[pp-1];
		
	}
	public String getParameter(String[] params, int p) {
		return getParameter(params,p,null);

	}
	
	public int getParameterInteger(String[] params, int p, int defaultValue) {
		return Integer.parseInt(getParameter(params, p, ""+defaultValue));
	}
	public int getParameterInteger(String[] params, int p) {
		return Integer.parseInt(getParameter(params, p));
	}
	
	public void setCommandContext(CommandContext ctx) {
		this.ctx = ctx;
		
	}
	public CommandContext getCommandContext() {
		return ctx;
	}
	public Context getContext() {
		return ctx.getContext();
	}
	
	public void setStartInSeparateThread(boolean b) {
		startInSeparateThread = b;
	}
	public boolean startInSeparateThread() {
		return startInSeparateThread;
	}
	
	public Set getPropertySet() {
		return props.keySet();
	}
	public Object getProperty(Object key) {
		return props.get(key);
	}
	public void setProperty(Object key, Object value){
		props.put(key, value);
	}
	public void setProperty(Object key, Object value, boolean localCall){
		if (localCall) {
			props.put(key, value);
		} else {
			externalProps.put(key, value);
		}
	}
	public void applyExternalProps() {
		props.putAll(externalProps);
		externalProps.clear();
	}
	public int adjustIntProperty(String pname, int pvalue) {
		String str = (String)getProperty(pname);
		int value = pvalue;
		try {
			Integer i = new Integer(str);
			int newValue = i.intValue();
			if (value!=newValue) {
				value = newValue;
				Printer.println(pname+" was set to "+newValue);
			}
		} catch (Exception ex) {
			Printer.println("Error: "+ ex.getMessage());
		}
		return value;
	}
}
